package org.smile.homework03.arthas.bank;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class Clerk {
    private Clerk approver;
    private String name;
    private Integer amount;
    List<Supervisor> supervisors = new ArrayList<>();

    public Clerk(Clerk approver, String name, Integer amount) {
        this.approver = approver;
        this.name = name;
        this.amount = amount;
    }

    public Clerk(String name) {
        this.name = name;
    }

    public void handleRequest(Integer number) {
        if (approver == null) {
            System.out.println(this.name + " : request is handled");
            notifySupervisors(number);
        } else {
            if (number < amount) {
                if (number > 10000) notifySupervisors(number);
                System.out.println(this.name + " : request is handled");
            } else {
                System.out.println(this.name + ": this request is passed to " + approver.getName());
                approver.handleRequest(number);
            }
        }
    }

    public void addSupervisor(Supervisor supervisor) {
        this.getSupervisors().add(supervisor);
    }

    public void notifySupervisors(int number) {
        for (Supervisor supervisor : supervisors) {
            supervisor.handMessage(number);
        }
    }

    public void handleObtain(int number) {
        if (number > 10000) new ProjectB().obtainCash(number);
        else new ProjectA().obtainCash(number);
    }
}
